package com.sd.database;

import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sd.database.BetaReadDatabaseHelper.Scan_TB;
import com.sd.database.BetaReadDatabaseHelper.Sreach_TB;

/**
 * 
 * 
 * @author Alex.Lu
 *
 */
public class BetaReadDao {
	
	
	/**
	 * 
	 * @param context
	 * @return
	 */
	public static Vector<String> querySearchItems(Context context){
		SQLiteDatabase db = BetaReadDatabaseHelper.getInstance(context).getReadableDatabase();
		String[] columns = {
				Sreach_TB.SREACH
		};
		Cursor c = db.query(Sreach_TB.TABLE_NAME, columns, null, null, null, null, "_id DESC LIMIT 10");
		Vector<String> ads = new Vector<String>();
		while(c.moveToNext()){
			String s= c.getString(c.getColumnIndex(Sreach_TB.SREACH));
			ads.add(s);
		}
		c.close();
		return ads;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	public static Vector<ScanItem> queryScanItems(Context context){
		SQLiteDatabase db = BetaReadDatabaseHelper.getInstance(context).getReadableDatabase();
		String[] columns = {
				Scan_TB._ID,
				Scan_TB.SCAN_NAME,
				Scan_TB.SCAN_URL
		};
		
		Cursor c = db.query(Scan_TB.TABLE_NAME, columns, null, null, null, null, "_id DESC LIMIT 10");
		Vector<ScanItem> ads = new Vector<ScanItem>();
		while(c.moveToNext()){
			ScanItem item = new ScanItem();
			item.name = c.getString(c.getColumnIndex(Scan_TB.SCAN_NAME));
			item.url = c.getString(c.getColumnIndex(Scan_TB.SCAN_URL));
			ads.add(item);
		}
		c.close();
		return ads;
	}
	
	/**
	 * 
	 * @param context
	 * @param item
	 */
	public static void insertScanItem(Context context,ScanItem item){
		SQLiteDatabase db = BetaReadDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put(Scan_TB.SCAN_NAME, item.name);
		values.put(Scan_TB.SCAN_URL, item.url);
		db.insert(Scan_TB.TABLE_NAME, null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	
	/**
	 * 
	 * @param context
	 * @param search
	 */
	public static void insertSearch(Context context,String search){
		SQLiteDatabase db = BetaReadDatabaseHelper.getInstance(context).getWritableDatabase();
		db.beginTransaction();
		ContentValues values = new ContentValues();
		values.put(Sreach_TB.SREACH, search);
		db.insert(Sreach_TB.TABLE_NAME, null, values);
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	
	
}
